/*用于加载页面中，学校类型加载，根据学校类型筛选学校和年级 (djf)*/
/*公共js*/
layui.use(['form'], function() {
	var form = layui.form;	
	/*加载学校类型下拉框*/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].id + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#selschooltype").append(str);
				form.render("select");
			}
		}
	});
	
	/*点击学校类型下拉框*/
	form.on('select(selschooltype)', function(data) {
		var schooltype = data.value;
		if(schooltype=="00" || schooltype==null || schooltype=='null'){
			schooltype="";
		}
		$("#selschool").html("");
		$("#selgradename").html("");
		//根据阶段筛选学校
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype='+schooltype,
			datatype : 'json',
			success : function(schooldata) {
				if (schooldata.code == 0) {
					var str = "<option value='00'>请选择学校</option>";
					for(var i = 0; i < schooldata.count; i++){
						str += '<option value=' + schooldata.data[i].schoolid + '>'+ schooldata.data[i].schoolName +'</option>';
					}
					$("#selschool").append(str);
					form.render("select");
				}
				else{
					layer.msg('暂无对应的学校！', function(){});
					form.render("select");
				}
				
			}
		});
		
		//根据学校类型，调用筛选年级方法
		switch (schooltype){
			case '10001':     //仅小学
				getGradeBySchoolType("../json/primary.json");
			break;
			case '10002':     //仅初中
				getGradeBySchoolType("../json/junior.json");
			break;     
			case '10003':     //仅高中
				getGradeBySchoolType("../json/high.json");
			break;
			case '10004':     //小学、初中
				getGradeBySchoolType("../json/primaryJunior.json");
			break;
			case '10005':    //初中、高中
				getGradeBySchoolType("../json/juniorHigh.json");
			break;
			case '10006':    //小学、初中、高中
				getGradeBySchoolType("../json/primaryJuniorHigh.json");
			break;
		}

	});
	
	/*动态加载年级下拉框(方法)*/
	function getGradeBySchoolType(url){
		$.ajax({
			type : 'get',
			url : url,
			datatype : 'json',
			success : function(gradedate) {
				if (gradedate.code == 0) {
					var str = "<option value='00'>请选择年级</option>";
					for(var i = 0; i < gradedate.count; i++){
						str += '<option value=' + gradedate.data[i].gradename + '>'+ gradedate.data[i].gradename+'</option>';
					}
					$("#selgradename").append(str);
					form.render("select");
				}
				else{
					layer.msg('暂无对应的年级！', function(){});
					form.render("select");
				}
			}
		});
	}
});


